package com.github.wj.security.server.service.impl;

import com.github.wj.security.server.domain.UserSysServiceRepository;
import com.github.wj.security.server.entity.SecurityUser;
import com.github.wj.security.server.entity.TSysUser;
import com.github.wj.security.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;

/**
 * Created by 王建 on 2017/6/7.
 */
public class UserServiceImpl implements UserService,UserDetailsService {

    @Autowired
    private UserSysServiceRepository repository;

    public TSysUser findByLoginId(String loginId) {
       List<TSysUser> tuser=repository.findByUserid(loginId);
       if(null!=tuser&&tuser.size()>0){
           return tuser.get(0);
       }
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //TUser对应数据库中的用户表可自定义
        TSysUser user = findByLoginId(s);
        if (user == null) {
            throw new UsernameNotFoundException("UserName " + s + " not found");
        }
        SecurityUser securityUser=new SecurityUser(user);
        return securityUser;
    }
}
